/*************************************************************************
 * File Name:    Binary_Tree_Inorder_Traversal.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: Sat 02 Nov 2013 03:00:23 PM CST
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Binary Tree Inorder Traversal
 | Given a binary tree, return the inorder traversal of its nodes' values.
 |
 | For example:
 | Given binary tree {1,#,2,3},
 | 1
 |  \
 |   2
 |  /
 | 3
 | return [1,3,2].
 |
 | Note: Recursive solution is trivial, could you do it iteratively?
 |
 | confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
 |
 | OJ's Binary Tree Serialization:
 | The serialization of a binary tree follows a level order traversal,
 | where '#' signifies a path terminator where no node exists below.
 |
 | Here's an example:
 |   1
 |  / \
 | 2   3
 |    /
 |   4
 |    \
 |     5
 | The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <iomanip>

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> ans;
        stack<TreeNode*> s;
        TreeNode *t;
        
        for (t = root; t != NULL; t = t->left) {
            s.push(t);
        }
        while (!s.empty()) {
            t = s.top(), s.pop();
            ans.push_back(t->val);
            t = t->right;
            while (t != NULL) {
                s.push(t);
                t = t->left;
            }
        }
        return ans;
    }
};
